$(function() {

    $.get(
        url + "User/clients",
        function(reg) {
            var html = "";
            if (reg.status == "success") {
                var n = reg.reson.length;
                for (var i = 0; i < reg.reson.length; i++) {
                    html += "<tr>";
                    html += "<td>" + reg.reson[i].id + "</td>";
                    html += "                                                        <td class=\"client-avatar\"><img alt=\"image\" src=\"img/a2.jpg\"> </td>";
                    html += "                                                        <td><a data-toggle=\"tab\" href=\"#contact-1\" class=\"client-link\">" + reg.reson[i].name + "</a>";
                    html += "                                                        </td>";
                    html += "                                                        <td> " + reg.reson[i].describe + "</td>";
                    html += "                                                        <td> " + tran(reg.reson[i].type) + "</td>";
                    html += "                                                        <td class=\"contact-type\"><i class=\"fa fa-phone\"> </i>";
                    html += "                                                        </td>";
                    html += "                                                        <td> " + reg.reson[i].phone + "</td>";
                    html += "                                                        <td width='160'><button  class='btn btn-info edit' type='button' data-toggle='modal' data-target='#myModal5'><i class='fa fa-paste'></i> 编辑</button>&nbsp;&nbsp;&nbsp;<button class='btn btn-danger admin' style='display:none;' type='button'>删除</button></td>";
                    html += "                                                    </tr>";
                }
                $("#clients").empty();
                $("#num").empty();
                $("#num").text(n);
                $("#clients").append(html);
                $(".edit").bind("click", function() {
                    $("#myModal").modal('show');
                    var uid = $(this).parents("tr").children("td").eq(0).text();
                    $.post(
                        url + "User/getClient", {
                            uid: uid
                        },
                        function(ret) {
                            if (ret.status == "success") {
                                $("#id").val(ret.reson.id);
                                $("#name").val(ret.reson.name);
                                $("#phone").val(ret.reson.phone);
                                $("#describe").text(ret.reson.describe);
                                if ($("input[name='sex']:checked").val() == ret.reson.sex) {
                                    
                                    $("input[name='sex'][value=" + ret.reson.sex + "]").attr("checked", "checked");
                                }
                                if ($("input[name='type']:checked").val() != ret.reson.type) {
                                    $("input[name='type'][value=" + ret.reson.type + "]").attr("checked", "checked");
                                }
                                if ($("input[name='is_often']:checked").val() != ret.reson.is_often) {
                                    $("input[name='is_often'][value=" + ret.reson.is_often + "]").attr("checked", "checked");
                                }
                            } else {
                                layer.msg(reg.reson, { icon: 2 });
                            }
                        }
                    );
                });
                if (flog == 1) {
                    $(".admin").css('display', "inline");
                }
            } else {
                layer.msg(reg.reson, { icon: 2 });
            }
        },
        'json'
    );


});

function tran(str) {
    return (str == 1) ? "客户" : "供货商";
}
$("#refresh").click(function() {
    window.location.reload();
});

// var v="";

// $(function(){
//     v = new Vue({
//         el:"#clientsaa",
//         data:{
//             edata:{}
//         },
//         methods:{
//             edit:function(event){
//                 console.log($(event));
//             }
//         }
//     });
// })

// function content(data){
var html = "";
html += "<div class=\"row\">";
html += "                <div class=\"col-sm-12\">";
html += "                    <div class=\"ibox float-e-margins\">";
html += "                        <div class=\"ibox-title\">";
html += "                            <h5>客户添加</h5>";
html += "                            <button type=\"button\" class=\"close\" data-dismiss=\"modal\"><span aria-hidden=\"true\">×</span><span class=\"sr-only\">关闭</span>";
html += "                            </button>";
html += "                        </div>";
html += "                        <div class=\"ibox-content\">";
html += "                            <form class=\"form-horizontal m-t\" id=\"commentForm\" data=\"false\" onsubmit=\"return false\">";
html += "                                <div class=\"form-group\">";
html += "                                    <label class=\"col-sm-3 control-label\">姓名：</label>";
html += "                                    <div class=\"col-sm-8\">";
html += "                                        <input id=\"cname\" name=\"name\" minlength=\"2\" type=\"text\" class=\"form-control\" required=\"\" aria-required=\"true\">";
html += "                                    </div>";
html += "                                </div>";
html += "                                <div class=\"form-group\">";
html += "                                    <label class=\"col-sm-3 control-label\">电话：</label>";
html += "                                    <div class=\"col-sm-8\">";
html += "                                        <input type=\"text\" class=\"form-control\" name=\"phone\" minlength=\"11\" maxlength=\"11\" required=\"\" aria-required=\"true\" onkeyup=\"this.value=this.value.replace(/\/\/D/g,\'\')\">";
html += "                                    </div>";
html += "                                </div>";
html += "                                <div class=\"form-group\">";
html += "                                    <label class=\"col-sm-3 control-label\">客户详细描述：</label>";
html += "                                    <div class=\"col-sm-8\">";
html += "                                        <textarea name=\"describe\" class=\"form-control\" required=\"\" aria-required=\"true\"></textarea>";
html += "                                    </div>";
html += "                                </div>";
html += "                                <div class=\"hr-line-dashed\"></div>";
html += "                                <div class=\"form-group\">";
html += "                                    <label class=\"col-sm-2 control-label\">性别</label>";
html += "                                    <div class=\"col-sm-10\">";
html += "                                        <div class=\"radio i-checks checkbox-inline\">";
html += "                                            <label>";
html += "                                                <input type=\"radio\" checked=\"true\" value=\"1\" name=\"sex\"> <i></i> 男</label>";
html += "                                        </div>";
html += "                                        <div class=\"radio i-checks checkbox-inline\">";
html += "                                            <label>";
html += "                                                <input type=\"radio\" value=\"2\" name=\"sex\"> <i></i>女</label>";
html += "                                        </div>";
html += "                                    </div>";
html += "                                </div>";
html += "                                <div class=\"form-group\">";
html += "                                    <label class=\"col-sm-2 control-label\">客户类型</label>";
html += "                                    <div class=\"col-sm-10\">";
html += "                                        <div class=\"radio i-checks checkbox-inline\">";
html += "                                            <label>";
html += "                                                <input type=\"radio\" checked=\"true\" value=\"1\" name=\"type\"> <i></i> 客户</label>";
html += "                                        </div>";
html += "                                        <div class=\"radio i-checks checkbox-inline\">";
html += "                                            <label>";
html += "                                                <input type=\"radio\" value=\"2\" name=\"type\"> <i></i>供应商</label>";
html += "                                        </div>";
html += "                                    </div>";
html += "                                </div>";
html += "                                <div class=\"form-group\">";
html += "                                    <label class=\"col-sm-2 control-label\">添加为常用客户</label>";
html += "                                    <div class=\"col-sm-10\">";
html += "                                        <div class=\"radio i-checks checkbox-inline\">";
html += "                                            <label>";
html += "                                                <input type=\"radio\" checked=\"true\" value=\"1\" name=\"is_often\"> <i></i> 普通客户</label>";
html += "                                        </div>";
html += "                                        <div class=\"radio i-checks checkbox-inline\">";
html += "                                            <label>";
html += "                                                <input type=\"radio\" value=\"2\" name=\"is_often\"> <i></i>常用客户</label>";
html += "                                        </div>";
html += "                                    </div>";
html += "                                </div>";
html += "                                <div class=\"hr-line-dashed\"></div>";
html += "                                <div class=\"form-group\">";
html += "                                    <label class=\"col-sm-2 control-label\">省份</label>";
html += "                                    <div class=\"col-sm-4\">";
html += "                                        <select id=\"region\" class=\"form-control m-b\" name=\"region\">";
html += "                                        </select>";
html += "                                    </div>";
html += "                                    <label class=\"col-sm-1 control-label\">地区</label>";
html += "                                    <div class=\"col-sm-4\">";
html += "                                        <select id=\"address\" class=\"form-control m-b\" name=\"address\">";
html += "                                        </select>";
html += "                                    </div>";
html += "                                </div>";
html += "                                <div class=\"hr-line-dashed\"></div>";
html += "                                <div class=\"form-group\">";
html += "                                    <label class=\"col-sm-2 control-label\"></label>";
html += "                                    <div class=\"col-sm-9\" id=\"submit\">";
html += "                                        <button type=\"submit\" class=\"btn btn-block btn-outline btn-primary\">保存信息</button>";
html += "                                    </div>";
html += "                                    <label class=\"col-sm-1 control-label\"></label>";
html += "                                </div>";
html += "                            </form>";
html += "                        </div>";
html += "                    </div>";
html += "                </div>";
html += "            </div>";
// }



// 默认的省份市区
var dataroot = Jurl + "/address.json";
$.getJSON(dataroot, function(data) {
    var region = data.region;
    var address = data.address;
    var html = "";
    for (var i = 0; i < region.length; i++) {
        html += "<option value='" + region[i].name + "' data='" + region[i].ProID + "'>" + region[i].name + "</option>";
    }
    $("#region").empty();
    $("#region").append(html);
    var regionId = $("#region  option:selected").attr('data');
    var html1 = "";
    for (var i = 0; i < address.length; i++) {
        if (regionId == address[i].ProID) {
            html1 += "<option value='" + address[i].name + "'>" + address[i].name + "</option>";
        }
    }
    $("#address").empty();
    $("#address").append(html1);
});

//下拉框改变触发事件
$("#region").on('change', function() {
    var regionId = $("#region  option:selected").attr('data');
    var dataroot = Jurl + "/address.json";
    $.getJSON(dataroot, function(data) {
        var address = data.address;
        var html1 = "";
        for (var i = 0; i < address.length; i++) {
            if (regionId == address[i].ProID) {
                html1 += "<option value='" + address[i].name + "'>" + address[i].name + "</option>";
            }
        }
        $("#address").empty();
        $("#address").append(html1);
    });
});
